package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Examination;
import com.itheima.service.ExaminationService;
import com.itheima.utils.AliOssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * []
 *
 * @author : [itnan]
 * @version : [v1.0]
 * @createTime : [2025/2/11 18:09]
 */

@RestController
@RequestMapping("/examination")
public class ExaminationController {

    @Reference
    private ExaminationService examinationService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询或分页条件查询体检信息
     */
    @GetMapping("/findPage")
    public PageResult findPage(QueryPageBean queryPageBean) {
        return examinationService.findPageOrCondition(queryPageBean);
    }

    /**
     * 根据id查询体检信息
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Examination examination = null;
        try {
            examination = examinationService.findExaminationById(id);
            return new Result(true, MessageConstant.QUERY_EXAMINATION_SUCCESS, examination);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_EXAMINATION_FAIL);
        }
    }

    @PutMapping
    public Result updateExamination(@RequestBody Examination examination) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            examinationService.updateExamination(examination);
            jedis.del("examination:" + examination.getPhoneNumber());
            return new Result(true, MessageConstant.EDIT_EXAMINATION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_EXAMINATION_FAIL);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    @PostMapping
    public Result add(@RequestBody Examination examination) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            examinationService.add(examination);
            jedis.del("examination:" + examination.getPhoneNumber());
            return new Result(true, MessageConstant.ADD_EXAMINATION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_EXAMINATION_FAIL);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    @DeleteMapping
    public Result deleteExaminationById(Integer id) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Examination examination = examinationService.findExaminationById(id);
            examinationService.deleteExaminationById(id);
            jedis.del("examination:" + examination.getPhoneNumber());
            return new Result(true, MessageConstant.DELETE_EXAMINATION_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_EXAMINATION_FAIL);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("examinationImg") MultipartFile examinationImg){
        try {
            String originalFilename = examinationImg.getOriginalFilename();
            int index = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(index);
            String fileName = UUID.randomUUID().toString() + suffix;
            String url = AliOssUtils.upload(examinationImg.getBytes(), fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, url);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @DeleteMapping("/deleteImg")
    public Result deleteImg(@RequestParam("examinationImg") String examinationImg){
        try {
            AliOssUtils.delete(examinationImg);
            return new Result(true, MessageConstant.PIC_DELETE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_DELETE_FAIL);
        }
    }
}
